/*
 * Mailman is used for holding mail from message passing, also provides
 * and array for blocking receives.
 * 
 * mailman.c
 */

#include <stdio.h>
#include <stdlib.h>
#include "mailman.h"

/*
 * Initiate the mailman.
 */
void initiateMailMan(MAILMAN *mailman)
{
    int i,j,k;

    for (i=0 ; i<10 ; i++) {
        for (j=0 ; j<10 ; j++) {
            for (k=0 ; k<4 ; k++) {
                mailman -> messages[i][i][0] = 'X';
            }
            mailman -> sProc[i][j] = 0;
        }
    }
}

/*
 * Checking if the process can receive mail.
 */
int isBlocked(MAILMAN *mailman, int process, char operand[])
{
    int sender, count = 0;
    if(operand[0] == 'X')
    {
        // Check if someone has sent to the receiving process
        for (sender=0 ; sender<10 ; sender++) {
            if(mailman->sProc[sender][process] == 1)
                count++;
        }

        // If there was a sender, return 0 or not blocked
        if(count >= 1)
            return 0;
        // Else block the process
        else
            return 1;
    } else {
        sender = atoi(operand);
        if(mailman->sProc[sender][process] == 1)
        {
            return 0;
        } else {
            return 1;
        }
    }
}

